-- cure_magic_skill
-- created by xinj
-- 施放恢复类技能时概率触发技能

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,
    -- 触发
    trigger = function(who, prop, arg)
        if who.type ~= OBJECT_TYPE_USER then
            return;
        end

        -- 不是恢复类技能
        if table.indexOf({11, 14, 43}, arg) < 1 then
            return;
        end

        -- 不满足概率
        if DungeonM.getRandSeed("cure_magic_skill") % 1000 >= prop[3] then
            return;
        end

        local target;
        local skillId = tonumber(prop[2]);
        local skills = SkillM.getSkillTable();
        if not skills[skillId] then
            return;
        end

        -- 目标
        local grid;
        if not SkillM.needTarget(skillId) then
            target = who;
        elseif not arg:isDead() then
            target = arg;
        else
            for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                grid = DungeonM.getGridByPos(p);

                -- 是未死亡的怪物
                if grid:isOpened() and
                    grid:isMonster() and
                    not grid.monster:isDead() then
                    target = grid.monster;
                    break;
                end
            end

            -- 已经没有目标了，不处理
            if not target then
                return;
            end
        end

        local round = CombatM.getRound();

        SkillM.apply(who, target, skillId, round);
    end,

    record = function(target, prop, path)
        return false;
    end
};
